package com.smartleanx.module.hrm.dal.mysql.employee;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.smartleanx.framework.mybatis.core.mapper.BaseMapperX;
import com.smartleanx.module.hrm.controller.admin.employee.vo.EmployeeDashboardPageReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeContractDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.eums.employee.EmployeeContractStatusEnum;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * 员工合同 Mapper
 *
 * @author tengcj
 */
@Mapper
public interface EmployeeContractMapper extends BaseMapperX<EmployeeContractDO> {

    default List<EmployeeContractDO> selectListByEmployeeId(Long employeeId) {
        return selectList(new LambdaQueryWrapper<EmployeeContractDO>()
        .eq(EmployeeContractDO::getEmployeeId, employeeId)
        .orderByDesc(EmployeeContractDO::getId));
    }

    default List<EmployeeContractDO> selectListByEmployeeIds(Collection<Long> employeeIds){
        return selectList(EmployeeContractDO::getEmployeeId,employeeIds);
    }

    default int deleteByEmployeeId(Long employeeId) {
        return delete(EmployeeContractDO::getEmployeeId, employeeId);
    }

    default PageResult<EmployeeContractDO> selectContractExpirationPage(EmployeeDashboardPageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<EmployeeContractDO>()
                .inIfPresent(EmployeeContractDO::getId, pageReqVO.getId())
                .inIfPresent(EmployeeContractDO::getEmployeeId,pageReqVO.getEmployeeId())
                .orderByDesc(EmployeeContractDO::getId));
    }
    default int updateContract(EmployeeContractDO contractDO){
        LambdaUpdateWrapper<EmployeeContractDO> updateWrapper = Wrappers.lambdaUpdate();
        if (contractDO.getEndTime() == null) {
            updateWrapper.set(EmployeeContractDO::getEndTime, null);
        }
        if (contractDO.getTerm() == null) {
            updateWrapper.set(EmployeeContractDO::getTerm, null);
        }
        updateWrapper.eq(EmployeeContractDO::getId, contractDO.getId());
      return update(contractDO, updateWrapper);
    }
}
